package com.pyk.service.impl;

import com.pyk.dto.Create_homework;
import com.pyk.mapper.CreateHomeworkQuestionMapper;
import com.pyk.mapper.HomeworkMapper;
import com.pyk.mapper.QuestionMapper;
import com.pyk.entity.Homework;
import com.pyk.entity.Question;
import com.pyk.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class HomeworkServiceImpl implements HomeworkService {

    @Autowired
    private HomeworkMapper homeworkMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private CreateHomeworkQuestionMapper createHomeworkQuestionMapper;

    @Override
    public List<Homework> list() {
        return homeworkMapper.list();
    }

    @Override
    public void add(Homework homework) {
        homework.setCreateTime(LocalDateTime.now());
        homework.setUpdateTime(LocalDateTime.now());
        homeworkMapper.insert(homework);
    }

    @Override
    public void deleteHomeworkById(Integer homeworkId)
    {
        createHomeworkQuestionMapper.deleteHomeworkById(homeworkId);
        homeworkMapper.deleteHomeworkById(homeworkId);
    }

    @Override
    public Homework getById(Integer homeworkId) {
        Homework homework = homeworkMapper.getById(homeworkId);
        return homework;
    }

    @Override
    public void update(Homework homework) {
        homework.setUpdateTime(LocalDateTime.now());
        homeworkMapper.update(homework);
    }

    @Transactional
    public void createHomeworkWithQuestions(Homework homework, List<Question> questions, int totalScorePerQuestion) {
        // 插入作业
        homeworkMapper.InsertHomework(homework);
        Integer homeworkId = homework.getHomeworkId(); // 假设插入后ID会自动设置回homework对象

        // 插入或查找每个题目，获取ID（这里假设总是插入新题目）
        List<Create_homework> createHomeworks = new ArrayList<>();
        for (Question question : questions) {
            questionMapper.insert(question);
            Integer questionId =  question.getQuestionId().intValue(); // 数据库已经让ID自增

            Create_homework createHomework = new Create_homework();
            createHomework.setHomeworkId(homeworkId);
            createHomework.setQuestionId(questionId);
            createHomework.setTotalScore(totalScorePerQuestion);

            createHomeworks.add(createHomework);
        }

        // 批量插入作业和题目的关联
        for (Create_homework createHomework : createHomeworks) {
            createHomeworkQuestionMapper.insertHomeworkQuestionAssociation(createHomework);
        }
    }

    @Override
    public void deleteHwQt(Integer homeworkId, Integer questionId) {
        createHomeworkQuestionMapper.deleteHwQt(homeworkId,questionId);
    }

    @Override
    public void updateHomeworkQ(Integer homeworkId, Long questionId, Question updatedQuestion) {
        questionMapper.updateById(updatedQuestion);
    }


}
